package com.pdwl.cashoutpay.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TimeUtil {
	
	/**
	 * 按照指定格式获取当前时间 test2
	 * @param model
	 * @return
	 */
	//设置通用的日期格式
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	
	public static String getTime(String model) {
		String date = null;
//		try {
			date = new SimpleDateFormat(model).format(new Date());
//		} catch (Exception e) {
//			throw new UntilException("9201", "getTime失败");
//		}
		return date;
	}
	
	/**
	 * 时间转换
	 * @param time
	 * @param num
	 * @param type DATE-按天；MONTH-按月；YEAR-按年
	 * @return
	 * @throws ParseException
	 */
	public static String timeShift(String time,int num,String type) throws ParseException{
		SimpleDateFormat sdf  =   new  SimpleDateFormat("yyyyMMdd"); 
		Date date = sdf.parse(time);
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		if("DATE".equals(type)){
			cd.add(Calendar.DATE, num);
			return sdf.format(cd.getTime());
		}else if("MONTH".equals(type)){
			cd.add(Calendar.MONTH, num);
			return sdf.format(cd.getTime());
		}else if("YEAR".equals(type)){
			cd.add(Calendar.YEAR, num);
			return sdf.format(cd.getTime());
		}
		return null;
	}
	
	/**
	 * 时间转换按天
	 * @param time
	 * @param num
	 * @return
	 * @throws ParseException
	 */
	public static String timeShiftDATE(String time,int num) throws ParseException{
		return timeShift(time, num, "DATE");
		
	}
	
	/**
	 * 将时间yyyyMMdd转换为指定格式
	 * @param str
	 * @param style
	 * @return
	 * @throws ParseException
	 */
	public static String format8(String str,String style) throws ParseException{
		SimpleDateFormat sdf  =   new  SimpleDateFormat("yyyyMMdd");
		Date data = sdf.parse(str);
		sdf  =   new  SimpleDateFormat(style);
		str = sdf.format(data);
		return str;
	}
	
	/**
	 * 将时间yyyyMMddHHmmss转换为指定格式
	 * @param str
	 * @param style
	 * @return
	 * @throws ParseException
	 */
	public static String format16(String str,String style) throws ParseException{
		SimpleDateFormat sdf  =   new  SimpleDateFormat("yyyyMMddHHmmss");
		Date data = sdf.parse(str);
		sdf  =   new  SimpleDateFormat(style);
		str = sdf.format(data);
		return str;
	}
	
	/**
	 * 将时间yyyyMMdd转换为yyyy年MMdd
	 * @param str
	 * @param style
	 * @return
	 * @throws ParseException
	 */
	public static String format8Norm(String str) throws ParseException{
		return format8(str, "yyyy年MM月dd日");
	}
	
	/**
	 * 将时间yyyyMMddHHmmss转换yyyy年MM月dd日 HH时mm分ss秒
	 * @param str
	 * @param style
	 * @return
	 * @throws ParseException
	 */
	public static String format16Norm(String str) throws ParseException{
		return format16(str, "yyyy年MM月dd日 HH时mm分ss秒");
	}
	
	
	/**
	 * 时间转换
	 * @param time
	 * @param num
	 * @param type DATE-按天；MONTH-按月；YEAR-按年;HOUR-按时；MINUTE-按分；SECOND-按秒
	 * @return
	 * @throws ParseException
	 */
	public static String timeShift2(String time,int num,String type) throws ParseException{
		SimpleDateFormat sdf  =   new  SimpleDateFormat("yyyyMMddHHmmss"); 
		Date date = sdf.parse(time);
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		if("DATE".equals(type)){
			cd.add(Calendar.DATE, num);
			return sdf.format(cd.getTime());
		}else if("MONTH".equals(type)){
			cd.add(Calendar.MONTH, num);
			return sdf.format(cd.getTime());
		}else if("YEAR".equals(type)){
			cd.add(Calendar.YEAR, num);
			return sdf.format(cd.getTime());
		}else if("HOUR".equals(type)){
			cd.add(Calendar.HOUR, num);
			return sdf.format(cd.getTime());
		}else if("MINUTE".equals(type)){
			cd.add(Calendar.MINUTE, num);
			return sdf.format(cd.getTime());
		}else if("SECOND".equals(type)){
			cd.add(Calendar.SECOND, num);
			return sdf.format(cd.getTime());
		}
		return null;
	}
	
	/**
	* 获取日期年份
	* @param date
	* @return
	* @throws ParseException
	*/
	public static int getYear(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return calendar.get(Calendar.YEAR);
	}
	
	/**
	* 获取日期月份
	* @param date
	* @return
	* @throws ParseException
	*/
	public static int getMonth(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return (calendar.get(Calendar.MONTH) + 1);
	}
	
	/**
	* 获取日期号
	* @param date
	* @return
	* @throws ParseException
	*/
	public static int getDay(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	/**
	* 获取月份起始日期
	* @param date
	* @return
	* @throws ParseException
	*/
	public static String getMinMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return 	(dateFormat.format(calendar.getTime())).substring(0,8)+"000000"; 
	}
	
	/**
	* 获取月份最后日期
	* @param date
	* @return
	* @throws ParseException
	*/
	public static String getMaxMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return 	(dateFormat.format(calendar.getTime())).substring(0,8)+"235959"; 
	}

}
